from rest_framework.response import Response
from z_Userapp.models import StorageModel, NestedList, MessageModel


class Inseries:

    def __init__(self, start, id):
        self.start = start
        self.id = id

    def execute(self, now):
        if self.start == 2:
            # 按条件清空三方表
            StorageModel.objects.filter(id=self.id).update(now_check=now)
            NestedList.objects.filter(sid_id=self.id).delete()
            NestedList.objects.create(sid_id=self.id, uid=now)
        else:
            StorageModel.objects.filter(id=self.id).update(now_check=now)
            NestedList.objects.filter(sid_id=self.id).delete()
            NestedList.objects.create(sid_id=self.id, uid=now)


class Inparallel:
    def __init__(self, start, id):
        self.start = start
        self.id = id

    def execute(self, now):
        if self.start == 2:
            # 按条件清空三方表
            StorageModel.objects.filter(id=self.id).update(now_check=now)
            NestedList.objects.filter(sid_id=self.id).delete()
            for i in now:
                NestedList.objects.create(sid_id=self.id, uid=i)
        else:
            StorageModel.objects.filter(id=self.id).update(now_check=now)
            NestedList.objects.filter(sid_id=self.id).delete()
            for i in now:
                NestedList.objects.create(sid_id=self.id, uid=i)


class SimpleFactory:
    @staticmethod
    def PlantExecute(start, id):
        storage = StorageModel.objects.filter(id=id).first()
        if start == 2:
            index = eval(storage.checklist).index(eval(storage.now_check))
            if len(eval(storage.checklist)) == index + 1:
                StorageModel.objects.filter(id=id).update(start=2)
                NestedList.objects.filter(sid_id=id).delete()
            else:
                storage1 = eval(storage.checklist)[index + 1]
                if type(storage1) == list:
                    storage2 = Inparallel(start, id)
                    storage2.execute(storage1)
                    #  并审的消息盒子一个人审完 另一个人不显示
                    MessageModel.objects.filter(wid_id=storage.id).update(read=1)
                else:
                    storage3 = Inseries(start, id)
                    storage3.execute(storage1)
            return Response({'msg': '审核通过', 'code': 200})
        else:
            index = eval(storage.checklist).index(eval(storage.now_check))
            if index == 0:
                StorageModel.objects.filter(id=id).update(start=3)
                NestedList.objects.filter(sid_id=id).delete()
            else:
                storage1 = eval(storage.checklist)[index - 1]
                if type(storage1) == list:
                    storage2 = Inparallel(start, id)
                    storage2.execute(storage1)
                else:
                    storage3 = Inseries(start, id)
                    storage3.execute(storage1)
            return Response({'msg': '审核未通过', 'code': 400})
